package com.ebanma.cloud.usertestall.config;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

/**
 * Caffeine缓存配置
 *
 * @author liuhe41
 * @date 2023/03/24
 */
@Configuration
@EnableCaching
public class CaffeineCacheConfig {

    //@Bean("cacheManager")
    //public CacheManager cacheManager() {
    //
    //    //构建缓存管理器
    //    SimpleCacheManager cacheManager = new SimpleCacheManager();
    //    //构建缓存集合
    //    ArrayList<CaffeineCache> caches = new ArrayList<>();
    //    //建立个缓存key为"user-cache"并设置
    //    caches.add(new CaffeineCache("user-cache", Caffeine.newBuilder()
    //            // 指定Key下的最大缓存数据量
    //            .maximumSize(1024)
    //            // 最后一次访问之后 120秒 过期
    //            .expireAfterAccess(120, TimeUnit.SECONDS)
    //            .build()));
    //
    //    cacheManager.setCaches(caches);
    //    return cacheManager;
    //}

    @Bean("cacheManager")
    public CacheManager cacheManager() {
        SimpleCacheManager cacheManager = new SimpleCacheManager();
        cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("user-cache")));
        return cacheManager;
    }
}
